<?php

	namespace app\controller;

	use app\Request;
	use app\BaseController;
	use app\service\ExcelData;
	use PhpOffice\PhpSpreadsheet\IOFactory;
	use PhpOffice\PhpSpreadsheet\Spreadsheet;
	use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
	use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
	use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
	use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

	class Index extends BaseController
	{
		protected Worksheet $worksheet;//活动表单
		protected array $title_name;//标题名称

		protected array $field_name=[

		];

		public function index()
		{
			echo "123";
		}

		public function read(Request $request)
		{
			//获取文件
			$file = $request->file('excel');//
			$this->getExcel($file);
			//获取映射字段
			$param = $request->post();
			//获取指定表头行数获取标题
			$excel_header = $param['excel_header'];//表头行数
			$this->getHeader($excel_header);
			//获取数据表信息
			$this->getExcelData($excel_header);
			halt(123);
			//根据映射表获取对应的数据值
			$map_array = json_decode($param['excel_map'], true);
			dump($this->title_name);
			halt($map_array);




		}


		//获取文件，加载文件对象
		private function getExcel($file)
		{
			$inputFileName = $file->getPathname();
			$spreadsheet = IOFactory::load($inputFileName);//加载文件
			$this->worksheet = $spreadsheet->getActiveSheet();//获取活动列
		}

		//获取标题
		private function getHeader($excel_header)
		{
			$maxColumn = $this->worksheet->getHighestColumn();
			$highestColumnIndex = Coordinate::columnIndexFromString($maxColumn); //获取列数量
			for ($i = 1; $i <= $highestColumnIndex; ++$i) {
				$title_name = $this->worksheet->getCell([$i, $excel_header])->getValue();
				if (!empty($title_name)) {
					$this->title_name[] = $title_name;
				}
			}
		}

		//获取表格内容
		protected function getExcelData($excel_header){
			$service=new ExcelData();
			$service->setWorksheet($this->worksheet);//活动表
			$service->setExcelHeaderInt($excel_header);//表头行
			$service->run();
		}


	}
